package com.repair.controller;


import cn.hutool.core.bean.BeanUtil;
import com.repair.dto.RepairReportDto;
import com.repair.entity.RepairReport;
import com.repair.form.RepairReportAddForm;
import com.repair.form.RepairReportUpdateForm;
import com.repair.form.RepairReportFindForm;
import com.repair.param.RepairReportAddParam;
import com.repair.param.RepairReportFindParam;
import com.repair.param.RepairReportUpdateParam;
import com.repair.service.RepairReportService;
import com.repair.util.JwtUtil;
import com.repair.util.PageInfo;
import com.repair.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 报修项目表 前端控制器
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@RestController
@RequestMapping("/repairReport")
@Api(tags = "报修项目接口")
public class RepairReportController {
    @Autowired
    private RepairReportService repairReportService;

    /**
     * 添加报修项目
     * @param form 添加报修项目信息，包含报修项目名称，是否需要付费，付费金额
     * @param token 添加报修的人员token
     * @return 返回添加结果
     */
    @PostMapping("/add")
    @ApiOperation("添加报修项目")
    public Result add(@RequestBody RepairReportAddForm form, @RequestHeader String token){
        Integer adminId = (Integer) JwtUtil.getData(token, "id");
        RepairReport repairReport = repairReportService.add(BeanUtil.toBean(form, RepairReportAddParam.class),adminId);
        return Result.success("添加报修项目表成功",repairReport);
    }

    /**
     * 修改报修项目信息
     * @param form 修改的报修内容，包含报修项目id，报修项目名称，是否需要付费，付费金额
     * @return 返回修改后的内容
     */
    @PostMapping("/update")
    @ApiOperation("修改报修项目信息")
    public Result update(@RequestBody RepairReportUpdateForm form){
        RepairReport repairReport =  repairReportService.updateMessage(BeanUtil.toBean(form, RepairReportUpdateParam.class));
        return Result.success("修改报修项目信息成功",repairReport);
    }

    /**
     * 删除报修项目
     * @param repairId 要删除的报修项目id
     * @return 返回是否删除成功
     */
    @PostMapping("/delete")
    @ApiOperation("删除报修项目")
    public Result delete(Integer repairId){
        repairReportService.delete(repairId);
        return Result.success("删除报修项目成功",null);
    }

    /**
     * 根据id查询报修项目
     * @param repairId 要查询的报修项目id
     * @return  返回查询到的信息
     */
    @ApiOperation("id查询报修项目")
    @GetMapping("/findById")
    public Result findById(Integer repairId){
        RepairReport repairReport =  repairReportService.findById(repairId);
        return Result.success("根据id查询报修项目成功",repairReport);
    }

    /**
     * 条件查询报修项目
     * @param form 查询条件，包含报修项目名称，是否需要付费，分页信息
     * @return 返回查询到的信息
     */
    @GetMapping("/findByCondition")
    @ApiOperation("条件查询报修项目")
    public Result findByCondition(RepairReportFindForm form){
        PageInfo<RepairReportDto> pageInfo =  repairReportService.findByCondition(BeanUtil.toBean(form, RepairReportFindParam.class));
        return Result.success("条件查询报修项目信息成功",pageInfo);
    }


}

